//
//  FirmwareInformationViewModel.swift
//  DaRings
//
//  Created by 易先生 on 2022/10/31.
//

import UIKit
import RxSwift
import RxCocoa

public enum FirmwareInformationAction{
    case name
    case color
    case size
    case version
    case mac
    case code
    
    public var title:String{
        switch self {
        case .name:
            return "firmware_device_name_title".language
        case .version:
            return "firmware_version_title".language
        case .mac:
            return "firmware_mac_address_title".language
        case .code:
            return "firmware_code_hash_title".language
        case .color:
            return "firmware_device_color_title".language
        case .size:
            return "firmware_device_size_title".language
        }
    }
}


public struct FirmwareInformationCelleModel {
    
    public var action : FirmwareInformationAction
        
    public var value : String?

}

open class FirmwareInformationViewModel: NSObject {
    
    public let provider = OTAProvider()
    
    public let firmwareInformationItems = BehaviorRelay<[FirmwareInformationCelleModel]>(value: [])

    public override init() {
        super.init()
        getFirmwareInformationItems()
    }
    
    private func getFirmwareInformationItems() {
        var items : [FirmwareInformationCelleModel] = []
        
       let localName = BluetoothManager.shared.connectDiscovery?.localName
        
        let deviceNameModel = FirmwareInformationCelleModel(action: .name, value: localName)
        items.append(deviceNameModel)
        
        let colorModel = FirmwareInformationCelleModel(action: .color, value: "")
        items.append(colorModel)
        
        let sizeModel = FirmwareInformationCelleModel(action: .size, value: "")
        items.append(sizeModel)
        
        
        let versionModel = FirmwareInformationCelleModel(action: .version, value: "")
        items.append(versionModel)
        
        let macAddressModel = FirmwareInformationCelleModel(action: .mac, value: "")
        items.append(macAddressModel)
        
        let codeHashModel = FirmwareInformationCelleModel(action: .code, value: "")
        items.append(codeHashModel)
        
        firmwareInformationItems.accept(items)
        
        updateData()
    }
    
    private func updateData() {
        
        var items : [FirmwareInformationCelleModel] = []
        
        let deviceInfo = DeviceInfo.current()
        if let deviceName = deviceInfo.localName {
            let deviceNameModel = FirmwareInformationCelleModel(action: .name, value: deviceName)
            items.append(deviceNameModel)
        }
        if let info = deviceInfo.info {
            
            let colorModel = FirmwareInformationCelleModel(action: .color, value: info.color.name)
            items.append(colorModel)
            
            let sizeModel = FirmwareInformationCelleModel(action: .size, value: "\(info.size)")
            items.append(sizeModel)
            
        }
        
        if let version = deviceInfo.versoin {
            let versionModel = FirmwareInformationCelleModel(action: .version, value: version)
            items.append(versionModel)
        }
        if let mac = deviceInfo.adress {
            let macAddressModel = FirmwareInformationCelleModel(action: .mac, value: mac)
            items.append(macAddressModel)
        }
        if let hash = deviceInfo.gitHash {
            let codeHashModel = FirmwareInformationCelleModel(action: .code, value: hash)
            items.append(codeHashModel)
        }
        
        self.firmwareInformationItems.accept(items)
        
    }
    
}
